<?php
App::import('Helper', 'SectionAppHelper');
class SectionHelper extends SectionAppHelper {
	public $helpers = array ('Html','Javascript','Session');
	private $style = "
		ul#MenuMain { position: fixed; left: 0px; background: none; list-style:none; list-style-type: none; }
		ul.section { position: absolute; }
		ul.cms_toolbar { display: block;  background: url(../../img/firma/bg_toolbar.png) repeat; width: 38px; padding: 5px; margin: 0px;}
		ul.cms_toolbar li.cms-li { list-style:none; list-style-type: none;  display: block; float: left; width: 32px; height: 32px; margin: 3px; padding: 3px; background: url(../../img/firma/bg_toolbar.png) repeat; }
		ul.cms_toolbar li.cms-li a { display: block; padding: 0px; margin: 0px; width: 32px; height: 32px; }
		ul.cms_toolbar li.cms-li a img { padding: 0px; margin: 0px;  }
		ul.cms_toolbar li.cms-li:hover { background: #fff; }

	";

	private $script = "
		hs.graphicsDir = '/css/highslide/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.outlineType = 'rounded-white';
		hs.fadeInOut = true;
		hs.dimmingOpacity = 0.75;
		hs.headingEval = 'this.thumb.alt';
	";
	
	public function main(){
		if($this->Session->check("Auth.User.id")){
			$html_toolbar = "<ul id=\"MenuMain\" class=\"MM cms_toolbar\">	";
			if($this->Session->read("Auth.User.group") == 'admin'){
				$html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_refresh.png',array('title'=>__('Page Refresh',true))),'/'.$this->params['url']['url'],
				        array('escape'=>false,'class'=>''))."</li> ";
				$html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_properties.png',array('title'=>__('Page Properties',true))), 
			        array('plugin'=>null,'controller'=>'cms','action'=>'edit',$this->Session->read('page.Cm.id')),
			        array('escape'=>false,'class'=>'','onclick'=>"return hs.htmlExpand(this, { objectType: 'iframe',width:800,height:700 } )"))."</li>";
			}
		    if($this->Session->read("Auth.User.group") == 'admin'){
			    $html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_setup.png',array('title'=>__('Page Setup',true))), 
			        array('plugin'=>null,'controller'=>'cms','action'=>'index'),
			        array('escape'=>false,'class'=>'','onclick'=>"return hs.htmlExpand(this, { objectType: 'iframe' ,width:800,height:700} )"))."</li>";
				$html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_emplo.png',array('title'=>__('Administrators',true))), 
			        array('plugin'=>null,'controller'=>'employees','action'=>'index'),
			        array('escape'=>false,'class'=>'','onclick'=>"return hs.htmlExpand(this, { objectType: 'iframe',width:800,height:700 } )"))."</li>";	
				$html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_setup_zip.png',array('title'=>__('Backup',true))), 
			        array('plugin'=>'backup','controller'=>'backup','action'=>'compressed'),
			        array('escape'=>false,'class'=>''))."</li>";
				$html_toolbar .=  "<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/actions_logout.png',array('title'=>__('Logout',true))), 
			        array('plugin'=>false,'controller'=>'users','action'=>'logout'),
			        array('escape'=>false,'class'=>''))."</li>";
			}   	        			               	       
			$html_toolbar .=  "</ul>\n\n ";		
			
			$this->Html->styleBlock("{$this->style}",array('inline'=>false));
			
			$this->Html->script(	array('highslide-with-html.packed'),$options = array('inline'=>false));	
			
			$this->Javascript->codeBlock( $this->script."\n",$options = array('allowCache'=>false,'safe'=>false,'inline'=>false));
			
			return $html_toolbar;
		}
	}
	
	
	
	public function generate($ctp_id=null,$action=null,$params=null){
		if(is_null($action)){ // tradycyjne pobieranie sekcji - dla konkretnej strony
			if($this->params['controller']=='pages'){
				$action = (isset($this->params['pass'][0]))?$this->params['pass'][0]:'display';
			}else{
				$action = $this->params['action'];
			}
			$section_body = $this->_section_body($ctp_id);
			$section_id = $this->_section_id($ctp_id);
		}else{ // $this->Section->generate(1,"fotter-about-artdent"); stała sekcja dla całej strony
			$page_const_section = $this->CompCms->_section(array('controller'=>'pages','action'=>$action,'section'=>$ctp_id));
			$section_body = $page_const_section['CmsSection']['body'];
			$section_id = $page_const_section['CmsSection']['id'];
		}
	
		if(isset($params['nomenu']) && $params['nomenu'] == true){

			$html_toolbar = '';
		
		}else{
		
			$html_toolbar = "
			<ul class=\"cms_toolbar section\">
				<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_edit.png',array('title'=>__('Edit Page',true))), 
			        array('plugin'=>null,'controller'=>'cms_sections','action'=>'quick_edit',$this->params['controller'],$action,$ctp_id),
			        array('escape'=>false,'class'=>'','onclick'=>"return hs.htmlExpand(this, { objectType: 'iframe',width:800,height:700 } )"))."</li>";
		        
			    
			$html_toolbar .= (is_numeric($section_id)) ? "
				<li class=\"cms-li\">".$this->Html->link($this->Html->image('firma/page_delete.png',array('title'=>__('Delete Page',true))), 
			        array('plugin'=>null,'controller'=>'cms_sections','action'=>'quick_delete',$section_id),
			        array('escape'=>false,'class'=>''), __('Are you sure you want to delete this text?', true))."</li></ul>" : "</ul>";

		}
		if($this->Session->check("Auth.User.id")){       
	        if($this->Session->read("Auth.User.group") == 'admin' || $this->Session->read("Auth.User.group") == 'manag' || $this->Session->read("Auth.User.group") == 'copyr'){
				return "<div class=\"section_tools\">".$html_toolbar.$section_body."</div>";
	        }else{
	        	return "<div>".$section_body."</div>";
	        }
		}else{
			return "<div>".$section_body."</div>";
		}
        
	}
	

	private function _section_body($ctp_id){
		$sections = $this->Session->read('sections');
		return (is_array($sections) && array_key_exists($ctp_id, $sections)) ? $sections[$ctp_id]['CmsSection']['body'] : "";
	}
	private function _section_id($ctp_id){
		$sections = $this->Session->read('sections');
		return (is_array($sections) && array_key_exists($ctp_id, $sections)) ? $sections[$ctp_id]['CmsSection']['id'] : "";
	}
	

		
}